package com.jo.interceptor;

import com.jo.advisor.Advice;
import com.jo.advisor.MethodBeforeAdvice;
import com.jo.invocation.MethodInvocation;

/**
 * 方法调用之前拦截器，组合了MethodBeforeAdvice，实际的拦截逻辑让advice去实现
 * @author Jo
 * @date 2018/1/25
 */
public class BeforeMethodInterceptor implements MethodInterceptor, AdviseAdapter {

    private MethodBeforeAdvice beforeAdvice;

    public BeforeMethodInterceptor(MethodBeforeAdvice beforeAdvice) {
        this.beforeAdvice = beforeAdvice;
    }

    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
        beforeAdvice.before(mi.getMethod(), mi.getArguments(), mi.getTarget());
        return mi.proceed();
    }


    @Override
    public boolean support(Advice advice) {
        return advice instanceof MethodBeforeAdvice;
    }

    @Override
    public MethodInterceptor convert(Advice advice) {
        return this;
    }
}
